package com.liusand.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liusand.common.thread.RedisCacheThread;
import com.liusand.model.pojo.Department;
import com.liusand.mapper.DepartmentMapper;
import com.liusand.model.vo.DepartmentVo;
import com.liusand.model.vo.PatientVo;
import com.liusand.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 科室 服务实现类
 * </p>
 *
 * @author liusand
 * @since 2023-01-29
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public List<DepartmentVo> listInMatrix(String page, String pageSize) {

        String cache = redisTemplate.opsForValue().get("HEALTH_CACHE:DEPARTMENT_" + page + "_" + pageSize);
        if(StringUtils.isNotEmpty(cache)){
            return JSON.parseArray(cache, DepartmentVo.class);
        }

        IPage<Department> newDepartmentPage = new Page<>();
        newDepartmentPage.setPages(Long.parseLong(page));
        newDepartmentPage.setSize(Long.parseLong(pageSize));
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getHospitalId, 1);
        wrapper.eq(Department::getState, 1);
        IPage<Department> departmentPage = departmentMapper.selectPage(newDepartmentPage, wrapper);
        List<Department> departmentList = departmentPage.getRecords();
        Map<Integer, List<Department>> map = departmentList.stream().filter(Objects::nonNull).filter(d -> d.getParent() != null).collect(Collectors.groupingBy(Department::getParent));
        List<Department> topDepartment = departmentList.stream().filter(Objects::nonNull).filter(d -> d.getParent() == null).collect(Collectors.toList());
        List<DepartmentVo> departmentVoList = new ArrayList<>();
        for(Department department : topDepartment){
            DepartmentVo vo = new DepartmentVo();
            BeanUtils.copyProperties(department, vo);
            vo.setChild(map.get(vo.getId()));
            departmentVoList.add(vo);
        }

        Thread thread = new Thread(new RedisCacheThread(redisTemplate, "HEALTH_CACHE:DEPARTMENT_" + page + "_" + pageSize, JSON.toJSONString(departmentVoList)));
        thread.start();
        return departmentVoList;
    }
}
